package com.fau.om.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fau.om.util.SystemConfig;

/**
 * @desc 用户实体类
 * @author kangyj
 * @datetime 2014年10月22日,下午6:28:20
 */
@Entity
@Table(name = "medical_admin")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "atsoft_cache")
public class MedicalAdmin implements UserDetails, Serializable, JSONAware {

	private static final long serialVersionUID = -3171194456022681990L;
	
	private String id;						
	private String username;				//登录名
	private String name;					//用户名
	private String password;				//登录密码
	private String jobStatus;				//在职状态  0：在职 1：离职
	private String onlineStatus;			//在线状态  0：在线 1：离线
	private String enterpriseId;			//企业
	
	private String deptType; 				//部门
	private MedicalGroup group;				//组
	private String isLeader;				//组长  、组员
	private String mobile;					//联系方式
	private String phoneNumber;				//绑定座机号
	private String description;				//描述信息
	private Date createTime = new Date();	//注册时间
	private String createIp;				//注册ip
	private Date lastLoginTime;				//最后登录时间
	private String lastLoginIp;				//最后登录ip
	
	private String isSuper;					// 是否是超级管理员0否，1是
	
	private Set<MedicalRole> roleSet;
	
	private String status;	//工作状态

	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name = "name", length = 250)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}
	
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "medical_admin_role", joinColumns = {@JoinColumn(name = "admin_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "atsoft_cache")
	public Set<MedicalRole> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<MedicalRole> roleSet) {
		this.roleSet = roleSet;
	}

	@Override
	@Transient
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authorites = new ArrayList<GrantedAuthority>();
		for(MedicalRole role : roleSet){
			GrantedAuthority ga = new SimpleGrantedAuthority(role.getId());
			authorites.add(ga);
		}
		return authorites;
	}
	
	@Override
	@Column(name = "password", length = 250)
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	@Column(name = "username", length = 250)
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name = "job_status", length = 20)
	public String getJobStatus() {
		return jobStatus;
	}
	
	public void setJobStatus(String jobStatus) {
		this.jobStatus = jobStatus;
	}

	@Column(name = "online_status", length = 2)
	public String getOnlineStatus() {
		return onlineStatus;
	}

	public void setOnlineStatus(String onlineStatus) {
		this.onlineStatus = onlineStatus;
	}

	@Column(name = "enterprise_id", length = 32)
	public String getEnterpriseId() {
		return enterpriseId;
	}

	public void setEnterpriseId(String enterpriseId) {
		this.enterpriseId = enterpriseId;
	}
	
	@Column(name = "dept_type", length = 20)
	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "group_id")
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "atsoft_cache")
	public MedicalGroup getGroup() {
		return group;
	}

	public void setGroup(MedicalGroup group) {
		this.group = group;
	}
	
	@Column(name = "is_leader", length = 5)
	public String getIsLeader() {
		return isLeader;
	}

	public void setIsLeader(String isLeader) {
		this.isLeader = isLeader;
	}

	@Column(name = "mobile", length = 20)
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Column(name = "phone_number", length = 20)
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	@Column(name = "description", length = 500)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	@Column(name = "create_ip", length = 250)
	public String getCreateIp() {
		return createIp;
	}

	public void setCreateIp(String createIp) {
		this.createIp = createIp;
	}

	@Column(name = "last_login_time")
	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	@Column(name = "last_login_ip", length = 255)
	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	@Column(name = "is_super", length = 5)
	public String getIsSuper() {
		return isSuper;
	}

	public void setIsSuper(String isSuper) {
		this.isSuper = isSuper;
	}

	@Override
	@Transient
	public boolean isAccountNonExpired() {
		return true;
	}
	
	@Override
	@Transient
	public boolean isAccountNonLocked() {
		return true;
	}
	
	@Override
	@Transient
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	@Override
	@Transient
	public boolean isEnabled() {
		return true;
	}

	@Transient
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("name", name);
		obj.put("username", username);
		obj.put("mobile", mobile);
		obj.put("phoneNumber", phoneNumber);
		obj.put("jobStatus", jobStatus);
		if(SystemConfig.ONLINE_STATUS.equals(onlineStatus)) {
			obj.put("lineStatus", "在线");
		} else if(SystemConfig.OFFLINE_STATUS.equals(onlineStatus)) {
			obj.put("lineStatus", "离线");
		}
		obj.put("status", status);
		obj.put("deptType", deptType);
		obj.put("description", description);
		obj.put("createTime", DateFormatUtils.format(createTime, "yyyy-MM-dd HH:mm:ss"));
		if(null != lastLoginTime){
			obj.put("lastLoginTime", DateFormatUtils.format(lastLoginTime, "yyyy-MM-dd HH:mm:ss"));
		}
		return obj.toJSONString();
	}
	

}
